using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manage_Default2 : System.Web.UI.Page
{
    protected Account acc;
    protected Functions func;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            firstLoad();
        }
    }

    private void firstLoad()
    {
        acc = new Account();
        DataTable tmp = acc.getAllAccountType();
        if (tmp.Rows.Count > 0)
        {
            ddlType.Items.Clear();
            for (int i = 0; i < tmp.Rows.Count; i++)
            {
                ddlType.Items.Add(new ListItem(tmp.Rows[i]["type_name"].ToString(), tmp.Rows[i]["ID"].ToString()));
            }
        }
        ddlType.DataBind();
    }
    //validate email
    protected void txtEmailCustomValidate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        acc = new Account();
        acc.email = txtEmail.Text;
        if (acc.checkEmail() && txtEmail.Text != "")
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    //Validate username
    protected void txtUserNameCustomValidate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        acc = new Account();
        acc.username = txtUserName.Text;
        if (acc.checkNameAccount() && txtUserName.Text != "")
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            acc = new Account();
            func = new Functions();
            acc.username = txtUserName.Text;
            acc.password = txtPass.Text;
            acc.name = txtName.Text;
            acc.birthday = func.getDateTime(tbDate.Text, ddlMonth.SelectedValue, tbYear.Text);
            acc.identify_card = txtIDCard.Text;
            acc.address = txtAddress.Text;
            acc.email = txtEmail.Text;
            acc.type_user = Convert.ToInt32(ddlType.SelectedValue.ToString());
            acc.link = func.RandomString(15, true);
            int user_id = acc.insert_user();
            if (user_id > 0)
            {
                if (func.confirmMail(acc.link, user_id, acc.email) > 0)
                {
                    Response.Redirect("./AccountNguoiDung.aspx");
                }
            }
        }
    }
}
